package com.jhh.pay.ysb.service.impl;

import java.util.HashMap;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.jhh.pay.channel.pojo.RequestBean;
import com.jhh.pay.channel.pojo.ResponseBean;
import com.jhh.pay.channel.pojo.Structure;
import com.jhh.pay.channel.service.PayService;
import com.loan_utils.util.HttpUrlPost;
import com.loan_utils.util.MD5Util;
import com.loan_utils.util.PropertiesReaderUtil;

/**
 * 2017/12/5.
 */
@Service
public class YsbPaymentServiceImpl implements PayService{

    private static final Logger logger = LoggerFactory.getLogger(YsbPaymentServiceImpl.class);

    private String platform = PropertiesReaderUtil.read("choospPlatform","ysb.platform");

    private String isTest = PropertiesReaderUtil.read("third", "isTest");

    // 获取商户号
    private String accountId = PropertiesReaderUtil.read("third", "accountId");
    // 获取加密用的key
    private String key = PropertiesReaderUtil.read("third", "key");

    private ResponseBean vaildResponse(String response){
    		JSONObject res = JSONObject.parseObject(response);
        String result_code = res.getString("result_code");
        String result_msg = res.getString("result_msg");
        ResponseBean resBean;
        if("0000".equals(result_code)){
            resBean = ResponseBean.SUCCESS_RESPONSE();
        }else{
            resBean = ResponseBean.FAIL_RESPONSE();
        }
        resBean.setMsg(result_msg);
		return resBean;
    }
    
	@Override
	public String getChannelName() {
		return "YIN_SHENG_BAO";
	}

	@Override
	public ResponseBean pay(RequestBean request) {
		//获取用户基本信息
//        PersonMode p = personMapper.getPersonInfo(String.valueOf(vo.getPerId()));
//
//        if (isTest.equals("on")) {
//            vo.setAmount("0.01");
//        }
        // 获取代付的地址
        String daifuUrl = PropertiesReaderUtil.read("third", "daifuUrl");
        // 回调地址
        String responseUrl = PropertiesReaderUtil.read("third", "payContCallBackUrl");

        String name = request.getBankInfo().getPersonalName();
        String cardNo = request.getExtension().get("cardNo");
        String orderId = request.getOrderNo();
        String purpose = "借款";
        String amount = String.valueOf(request.getMoney());
        // 进行加密得到mac签名
        StringBuffer macStr = new StringBuffer("accountId=" + accountId);
        macStr.append("&").append("name=" + name);
        macStr.append("&").append("cardNo=" + cardNo);
        macStr.append("&").append("orderId=" + orderId);
        macStr.append("&").append("purpose=" + purpose);
        macStr.append("&").append("amount=" + amount);
        macStr.append("&").append("responseUrl=" + responseUrl);
        macStr.append("&").append("key=" + key);
        // System.out.println(macStr.toString());
        String mac = MD5Util.encodeToMd5(macStr.toString()).toUpperCase();
        // 请求第三方接口
        Map<String, String> paramMap = new HashMap<>();
        paramMap.put("accountId", accountId);
        paramMap.put("name", name);
        paramMap.put("cardNo", cardNo);
        paramMap.put("orderId", orderId);
        paramMap.put("purpose", purpose);
        paramMap.put("amount", amount);
        paramMap.put("responseUrl", responseUrl);
        paramMap.put("mac", mac);
        logger.info("请求第三方支付参数====="+"orderId:"+ orderId+",amount:"+ amount+",cardNo:"+ cardNo+",name:"+ name+",requestUrl："+daifuUrl+",responseUrl："+ responseUrl);
        String response = HttpUrlPost.sendPost(daifuUrl, paramMap);
        logger.info("第三方支付响应=====orderId:"+orderId+",response:"+response);
        return vaildResponse(response);
	}

	@Override
	public ResponseBean deduct(RequestBean request) {
        //读取配置文件
        String url = PropertiesReaderUtil.read("third","collectUrl");
        String responseUrl =PropertiesReaderUtil.read("third","collectResponseUrl");
        String purpose = "还贷";
        String amount = String.valueOf(request.getMoney());
        String orderId = request.getOrderNo();
        String subContractId = request.getExtension().get("subContractId");
        String phone = request.getBankInfo().getBankMobile();
        String mac = "accountId="+accountId+"&subContractId="+subContractId+"&orderId="+orderId+
                "&purpose="+purpose+"&amount="+amount+"&phoneNo="+phone+
                "&responseUrl="+responseUrl+"&key="+key;
        String macMD5 = MD5Util.encodeToMd5(mac);

        //请求参数
        Map<String,String> param = new HashMap<String,String>();
        param.put("accountId", accountId);
        param.put("subContractId", subContractId);
        param.put("orderId", orderId);
        param.put("purpose", purpose);
        param.put("amount", amount);
        param.put("phoneNo", phone);
        param.put("responseUrl", responseUrl);
        param.put("mac", macMD5.toUpperCase());

        logger.info("请求第三方代扣参数====="+"orderId:"+ orderId+",amount:"+ amount+",phoneNo:"+ phone+",subContractId:"+ subContractId+",requestUrl："+url+",responseUrl："+ responseUrl);
        String response = HttpUrlPost.sendPost(url, param);
        logger.info("第三方代扣响应=====orderId:"+orderId+",response:"+response);
        return vaildResponse(response);
	}

	@Override
	public ResponseBean callbackResolve(RequestBean request) {
		throw new UnsupportedOperationException("并未实现");
	}

	@Override
	public ResponseBean state(RequestBean request) {
		return null;
	}

	@Override
	public Structure structure(RequestBean request) {
		
		return null;
	}

}
